/**
 * AdminIssueService的实现类
 *
 * @author JINMO
 * @since 2022/06/26 11:19
 */

package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.service.admin.AdminIssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class AdminIssueServiceImpl implements AdminIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    /**
     * 查询未删除的通用问题通过基本参数
     *
     *
     * @param question
     * @param baseParam 基本参数
     * @return {@link CommonData }<{@link MarketIssue }>
     * @author: jinmo
     * @date: 2022/06/26 11:20:27
     */
    @Override
    public CommonData<MarketIssue> queryCommonIssueByBaseParamAndQuestion(String question, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        //适配模糊查询
        if (!StringUtils.isEmpty(question)) {
            question = "%" + question.trim() + "%";
        }

        List<MarketIssue> marketIssues = marketIssueMapper.select(question,baseParam.getSort(), baseParam.getOrder());

        PageInfo<MarketIssue> marketIssuePageInfo = new PageInfo<>(marketIssues);
        return CommonData.data(marketIssuePageInfo);

    }

    /**
     * 建立通用问题
     *
     * @param question 问题
     * @param answer   回答
     * @return {@link CommonData }<{@link MarketIssue }>
     * @author: jinmo
     * @date: 2022/06/26 17:43:51
     */
    @Override
    public MarketIssue createCommonIssue(String question, String answer) {
        //新建MarketIssue，赋值
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        Date date = new Date();
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);
        //向数据库中插入数据，并为id赋值
        marketIssueMapper.insertWithId(marketIssue);

        return marketIssue;
    }


    /**
     * 更新通用问题
     *
     * @param marketIssue 市场问题
     * @return {@link MarketIssue }
     * @author: jinmo
     * @date: 2022/06/26 18:16:37
     */
    @Override
    public MarketIssue updateCommonIssue(MarketIssue marketIssue) {
        //更新对象
        marketIssue.setUpdateTime(new Date());
        //更新数据库
        marketIssueMapper.updateCommonIssue(marketIssue);

        return marketIssue;
    }


    /**
     * 更改数据库中通用问题达成删除效果
     *
     * @param marketIssue 市场问题
     * @author: jinmo
     * @date: 2022/06/26 18:41:02
     */
    @Override
    public void deleteCommonIssue(MarketIssue marketIssue) {
        marketIssue.setDeleted(true);
        //更新数据库中通用问题删除属性
        marketIssueMapper.updateCommonIssueDeleted(marketIssue);
    }
}
